前端页面调用后台接口,使用浏览器默认下载的方法。
public ResponseEntity<byte[]> test() throws Exception {
String Path = "";//要下载的文件路径
File file = new File(Path);
HttpHeaders headers = new HttpHeaders();
//要生成的文件
String filename = new String("test.txt".getBytes("utf-8"), "iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", filename);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(org.apache.commons.io.FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}